package net.blank.ui.core
{
	import flash.display.DisplayObjectContainer;
	import flash.text.engine.TextBlock;
	import flash.text.engine.TextJustifier;
	import flash.text.engine.TextLine;
	import flash.text.engine.TextRotation;
	
	import net.blank.pool.Pool;
	
	/**
	 * 文本创建器,基本文本块
	 * @author blank
	 * 
	 */
	public class TextCreatorForTextBlock
	{
		/**
		 * 创建水平文本,固定行间距
		 * @param textblocks
		 * @param textLines
		 * @param parent
		 * @param boundsX
		 * @param boundsY
		 * @param boundsWidth
		 * @param boundsHeight
		 * @param align
		 * @param verticalAlign
		 * @param gap
		 * @param textJustifier
		 * @return 
		 * 
		 */
		public static function createTextLines_h_fixedGap(
			textblocks:Vector.<TextBlock>,
			textLines:Vector.<TextLine>,
			parent:DisplayObjectContainer,
			boundsX:Number,
			boundsY:Number,
			boundsWidth:Number,
			boundsHeight:Number,
			align:String=Arrange.LEFT,
			verticalAlign:String=Arrange.TOP,
			gap:Number=4,
			textJustifier:TextJustifier=null
		):Boolean{
			TextCreator.releaseTextLines(textLines,parent);
			
			if(boundsWidth <= 0 || boundsHeight <= 0){
				return false;
			}
			
			/**是否将文本行创建完成*/
			var isCreatedAll:Boolean;
			var textHeight:Number=0;
			
			//创建文本行
			var n:int=0, i:int;
			var textLine:TextLine, recycleLine:TextLine;
			
			const LEN:int=textblocks.length;
			var textBlock:TextBlock;
			for(i=0;i<LEN;i++){
				textBlock=textblocks[i];
				
				while(true){
					recycleLine=Pool.restoreObj(TextLine);
					if(recycleLine){
						textLine=textBlock.recreateTextLine(recycleLine,textLine,boundsWidth);
					}else{
						textLine=textBlock.createTextLine(textLine,boundsWidth);
					}
					
					if(!textLine){
						isCreatedAll=true;
						break;
					}
					
					textHeight += (n == 0 ? textLine.totalAscent : textLine.totalHeight + gap);
					
					textLines[n++]=textLine;
					
					//如果这一行显示不完,则停止创建
					if(textHeight >= boundsHeight){
						break;
					}
				}
				
				textLine=null;
			}
			
			TextCreator.verticalArrange(textLines,parent,boundsX,boundsY,boundsWidth,boundsHeight,gap,textHeight,align,verticalAlign,isCreatedAll);
			
			return isCreatedAll;
		}
		
		/**
		 * 创建垂直文本,固定行间距
		 * @param contents
		 * @param textLines
		 * @param parent
		 * @param boundsX
		 * @param boundsY
		 * @param boundsWidth
		 * @param boundsHeight
		 * @param align
		 * @param horizontalAlign
		 * @param gap
		 * @param textJustifier
		 * @param enableLink
		 * @return 
		 * 
		 */
		public static function createTextLines_v_fixedGap(
			textblocks:Vector.<TextBlock>,
			textLines:Vector.<TextLine>,
			parent:DisplayObjectContainer,
			boundsX:Number,
			boundsY:Number,
			boundsWidth:Number,
			boundsHeight:Number,
			align:String=Arrange.TOP,
			horizontalAlign:String=Arrange.LEFT,
			gap:Number=4,
			textJustifier:TextJustifier=null
		):Boolean{
			TextCreator.releaseTextLines(textLines,parent);
			
			if(boundsWidth <= 0 || boundsHeight <= 0){
				return false;
			}
			
			/**是否将文本行创建完成*/
			var isCreatedAll:Boolean;
			var textWidth:Number=0;
			
			//创建文本行
			var n:int=0,i:int;
			var textLine:TextLine, recycleLine:TextLine;
			
			const LEN:int=textblocks.length;
			var textBlock:TextBlock;
			for(i=0;i<LEN;i++){
				textBlock=textblocks[i];
				
				//文本方向
				textBlock.lineRotation=TextRotation.ROTATE_90;
				textBlock.textJustifier=textJustifier || TextCreator.DEFAULT_TEXT_JUSTIFIER;
				
				while(true){
					recycleLine=Pool.restoreObj(TextLine);
					if(recycleLine){
						textLine=textBlock.recreateTextLine(recycleLine,textLine,boundsHeight);
					}else{
						textLine=textBlock.createTextLine(textLine,boundsHeight);
					}
					
					if(!textLine){
						isCreatedAll=true;
						break;
					}
					
					textWidth += (n == 0 ? textLine.totalAscent : textLine.width + gap);
					
					textLines[n++]=textLine;
					
					//如果这一行显示不完,则停止创建
					if(textWidth >= boundsWidth){
						break;
					}
				}
				
				textLine=null;
			}
			
			TextCreator.horizontalArrange(textLines,parent,boundsX,boundsY,boundsWidth,boundsHeight,gap,textWidth,align,horizontalAlign,isCreatedAll);
			
			return isCreatedAll;
		}
	}
}